//判断是否是质数
function isPrime (num){
    for(let i = 2;i < num;i++){
        if (num % i == 0){
            return true
        }
    }
    return false
}
console.log(isPrime(3));

//返回指定数的阶乘的值
function factorial (num){
    if(num < 0){
        throw new Error("请输入一个正整数")
    }else if (num == 1){
        return 1
    }
    return num * factorial(num - 1)
}
console.log(factorial(3));

//返回斐波那契数列的前n项的和（n为给定）
function fib (num){
    if(num < 0){
        throw new Error("请输入一个正整数")
    }else if (num == 1 || num == 2){
        return 1
    }
    return fib(num - 2) + fib(num - 1)
}

console.log(fib(6));

//判断数组是否被排序过
function isSort(arr) {
    let flag = true
    for(let i = 0;i < arr.length;i++){
        if((arr[i] < arr[i+1] && arr[i+1] > arr[i+2]) || (arr[i] > arr[i+1] && arr[i+1] < arr[i+2])){
            flag = false
            break
        }
    }
    return flag
}
console.log(isSort([1,2,3,4,5,7]));

//反转字符串
function reverse (str) {
    if(typeof str !== "string"){throw new Error("请传入一个字符串参数")} 
    let arr = str.split("")
    return arr.reverse().join("")
}
console.log(reverse("今天天气不错"));

//为什么这个代码会返回这样结果？
function isBig(thing) {
    if (thing == 0 || thing == 1 || thing == 2) {
      return false
    }
    return true
  }
  console.log(isBig(1));  // false
  console.log(isBig([2]));  // false
  console.log(isBig([3]));  // true
 
